{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 安装和基本概念\n",
    "\n",
    "安装方法：pip install plotly\n",
    "```\n",
    "import plotly\n",
    "import plotly.graph_objs as go\n",
    "```\n",
    "\n",
    "在Plotly中有两个基本模块\n",
    "1. plotly.plotly: 主要包含链接Ployly服务器的一些函数 \n",
    "2. plotly.graph_objs: 主要包含生成图像实例的函数\n",
    "\n",
    "在Ployly中使用三个不同的objects来定义一个图像，分别是：\n",
    "1. Data: 即数据，定义图像中使用的数据，通常为一个列表\n",
    "2. Layout: 即布局，主要定义图像的外观\n",
    "   - Annotations: 标记图像上一个特殊位置，通常为一个或者几个点\n",
    "   - Shapes: 标记图像上的一部分特殊位置，通常为一块区域\n",
    "3. Figure: 最终的图像实例，go.Figure帮助我们构建一个可显示的图像实例"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基本使用\n",
    "\n",
    "两种使用方法\n",
    "1. 在线模式，需要注册ploy.ly的账号，同时数据和图保存在云端账户中\n",
    "```\n",
    "plotly.tools.set_credentials_file(username='DemoAccount', api_key='lr1c37zw81')\n",
    "```\n",
    "\n",
    "2. 离线模式\n",
    "离线模式有两种使用方式：plotly.offline.plot() 和 plotly.offline.iplot()\n",
    "\n",
    "   - plotly.offline.plot() 会在本地创建一个标准的HTML文件，你可以通过浏览器打开\n",
    "```\n",
    "plotly.offline.plot({\n",
    "    \"data\": [go.Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1])],\n",
    "    \"layout\": go.Layout(title=\"hello world\")\n",
    "}, auto_open=True)\n",
    "```\n",
    "\n",
    "   - plotly.offline.iplot() 允许你使用 Jupyter Notebook 进行画图和展示\n",
    "```\n",
    "plotly.offline.init_notebook_mode(connected=True)\n",
    "plotly.offline.iplot({\n",
    "    \"data\": [go.Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1])],\n",
    "    \"layout\": go.Layout(title=\"hello world\")\n",
    "})\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<script type=\"text/javascript\">window.PlotlyConfig = {MathJaxConfig: 'local'};</script><script type=\"text/javascript\">if (window.MathJax) {MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}</script><script>requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window._Plotly) {require(['plotly'],function(plotly) {window._Plotly=plotly;});}</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<script type=\"text/javascript\">window.PlotlyConfig = {MathJaxConfig: 'local'};</script><script type=\"text/javascript\">if (window.MathJax) {MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}</script><script>requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window._Plotly) {require(['plotly'],function(plotly) {window._Plotly=plotly;});}</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 引入相关包\n",
    "import plotly\n",
    "import plotly.graph_objs as go\n",
    "plotly.offline.init_notebook_mode(connected=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "linkText": "Export to plot.ly",
        "plotlyServerURL": "https://plot.ly",
        "showLink": true
       },
       "data": [
        {
         "marker": {
          "color": "red",
          "size": 10,
          "symbol": 104
         },
         "mode": "markers+lines",
         "name": "1st Trace",
         "text": [
          "one",
          "two",
          "three"
         ],
         "type": "scatter",
         "uid": "8001a4e1-b929-4e4d-9578-0998e95f36c0",
         "x": [
          1,
          2,
          3
         ],
         "y": [
          4,
          5,
          6
         ]
        }
       ],
       "layout": {
        "title": "First Plot",
        "xaxis": {
         "title": "x1"
        },
        "yaxis": {
         "title": "x2"
        }
       }
      },
      "text/html": [
       "<div id=\"0ba69467-74f4-49f5-a35a-1165a9e9a1af\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"0ba69467-74f4-49f5-a35a-1165a9e9a1af\", [{\"marker\": {\"color\": \"red\", \"size\": 10, \"symbol\": 104}, \"mode\": \"markers+lines\", \"name\": \"1st Trace\", \"text\": [\"one\", \"two\", \"three\"], \"x\": [1, 2, 3], \"y\": [4, 5, 6], \"type\": \"scatter\", \"uid\": \"8001a4e1-b929-4e4d-9578-0998e95f36c0\"}], {\"title\": \"First Plot\", \"xaxis\": {\"title\": \"x1\"}, \"yaxis\": {\"title\": \"x2\"}}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"0ba69467-74f4-49f5-a35a-1165a9e9a1af\"));});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"0ba69467-74f4-49f5-a35a-1165a9e9a1af\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"0ba69467-74f4-49f5-a35a-1165a9e9a1af\", [{\"marker\": {\"color\": \"red\", \"size\": 10, \"symbol\": 104}, \"mode\": \"markers+lines\", \"name\": \"1st Trace\", \"text\": [\"one\", \"two\", \"three\"], \"x\": [1, 2, 3], \"y\": [4, 5, 6], \"type\": \"scatter\", \"uid\": \"8001a4e1-b929-4e4d-9578-0998e95f36c0\"}], {\"title\": \"First Plot\", \"xaxis\": {\"title\": \"x1\"}, \"yaxis\": {\"title\": \"x2\"}}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"0ba69467-74f4-49f5-a35a-1165a9e9a1af\"));});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "trace = go.Scatter(\n",
    "    x = [1,2,3], \n",
    "    y = [4,5,6], \n",
    "    marker = {'color': 'red', 'symbol': 104, 'size': 10}, \n",
    "    mode = \"markers+lines\",  \n",
    "    text = [\"one\",\"two\",\"three\"], \n",
    "    name = '1st Trace'\n",
    ")\n",
    "\n",
    "# plotly三要素\n",
    "data = [trace]\n",
    "layout = go.Layout(title=\"First Plot\", xaxis={'title':'x1'}, yaxis={'title':'x2'})\n",
    "figure = go.Figure(data=data, layout=layout)\n",
    "plotly.offline.iplot(figure, filename='pyguide_1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Figure({\n",
       "    'data': [{'marker': {'color': 'red', 'size': 10, 'symbol': 104},\n",
       "              'mode': 'markers+lines',\n",
       "              'name': '1st Trace',\n",
       "              'text': [one, two, three],\n",
       "              'type': 'scatter',\n",
       "              'uid': '51f7328b-c4c7-45dd-8a22-15110100e013',\n",
       "              'x': [1, 2, 3],\n",
       "              'y': [4, 5, 6]}],\n",
       "    'layout': {'title': 'First Plot', 'xaxis': {'title': 'x1'}, 'yaxis': {'title': 'x2'}}\n",
       "})"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 我们来看一下figure这个变量\n",
    "figure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "linkText": "Export to plot.ly",
        "plotlyServerURL": "https://plot.ly",
        "showLink": true
       },
       "data": [
        {
         "marker": {
          "color": "red",
          "size": 10,
          "symbol": 104
         },
         "mode": "markers+lines",
         "name": "1st Trace",
         "text": [
          "one",
          "two",
          "three"
         ],
         "type": "scatter",
         "uid": "51f7328b-c4c7-45dd-8a22-15110100e013",
         "x": [
          1,
          2,
          3
         ],
         "y": [
          4,
          5,
          6
         ]
        }
       ],
       "layout": {
        "title": "Plot update",
        "xaxis": {
         "title": "x1"
        },
        "yaxis": {
         "title": "x2"
        }
       }
      },
      "text/html": [
       "<div id=\"377233be-fc26-433f-bcac-3af6cf9df9a0\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"377233be-fc26-433f-bcac-3af6cf9df9a0\", [{\"marker\": {\"color\": \"red\", \"size\": 10, \"symbol\": 104}, \"mode\": \"markers+lines\", \"name\": \"1st Trace\", \"text\": [\"one\", \"two\", \"three\"], \"x\": [1, 2, 3], \"y\": [4, 5, 6], \"type\": \"scatter\", \"uid\": \"51f7328b-c4c7-45dd-8a22-15110100e013\"}], {\"title\": \"Plot update\", \"xaxis\": {\"title\": \"x1\"}, \"yaxis\": {\"title\": \"x2\"}}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"377233be-fc26-433f-bcac-3af6cf9df9a0\"));});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"377233be-fc26-433f-bcac-3af6cf9df9a0\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"377233be-fc26-433f-bcac-3af6cf9df9a0\", [{\"marker\": {\"color\": \"red\", \"size\": 10, \"symbol\": 104}, \"mode\": \"markers+lines\", \"name\": \"1st Trace\", \"text\": [\"one\", \"two\", \"three\"], \"x\": [1, 2, 3], \"y\": [4, 5, 6], \"type\": \"scatter\", \"uid\": \"51f7328b-c4c7-45dd-8a22-15110100e013\"}], {\"title\": \"Plot update\", \"xaxis\": {\"title\": \"x1\"}, \"yaxis\": {\"title\": \"x2\"}}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"377233be-fc26-433f-bcac-3af6cf9df9a0\"));});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 可以看到Figure实例实际上就是一个字典类型的变量，我们可以更新这个变量\n",
    "figure.update(dict(layout=dict(title='Plot update'), data=dict(marker=dict(color='blue'))))\n",
    "plotly.offline.iplot(figure, filename='pyguide_2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.plotly.v1+json": {
       "config": {
        "linkText": "Export to plot.ly",
        "plotlyServerURL": "https://plot.ly",
        "showLink": true
       },
       "data": [
        {
         "marker": {
          "color": "navy",
          "line": {
           "width": 1
          },
          "size": 12
         },
         "mode": "markers",
         "name": "Americas",
         "text": [
          "Country: Argentina<br>Population: 40301927.0",
          "Country: Bolivia<br>Population: 9119152.0",
          "Country: Brazil<br>Population: 190010647.0",
          "Country: Canada<br>Population: 33390141.0",
          "Country: Chile<br>Population: 16284741.0",
          "Country: Colombia<br>Population: 44227550.0",
          "Country: Costa Rica<br>Population: 4133884.0",
          "Country: Cuba<br>Population: 11416987.0",
          "Country: Dominican Republic<br>Population: 9319622.0",
          "Country: Ecuador<br>Population: 13755680.0",
          "Country: El Salvador<br>Population: 6939688.0",
          "Country: Guatemala<br>Population: 12572928.0",
          "Country: Haiti<br>Population: 8502814.0",
          "Country: Honduras<br>Population: 7483763.0",
          "Country: Jamaica<br>Population: 2780132.0",
          "Country: Mexico<br>Population: 108700891.0",
          "Country: Nicaragua<br>Population: 5675356.0",
          "Country: Panama<br>Population: 3242173.0",
          "Country: Paraguay<br>Population: 6667147.0",
          "Country: Peru<br>Population: 28674757.0",
          "Country: Puerto Rico<br>Population: 3942491.0",
          "Country: Trinidad and Tobago<br>Population: 1056608.0",
          "Country: United States<br>Population: 301139947.0",
          "Country: Uruguay<br>Population: 3447496.0",
          "Country: Venezuela<br>Population: 26084662.0"
         ],
         "type": "scatter",
         "uid": "c2bae803-f572-4c0a-8e45-6874fec3b870",
         "x": [
          12779.379640000001,
          3822.1370840000004,
          9065.800825,
          36319.235010000004,
          13171.63885,
          7006.580419,
          9645.06142,
          8948.102923,
          6025.374752000001,
          6873.262326000001,
          5728.353514,
          5186.050003,
          1201.637154,
          3548.3308460000003,
          7320.880262000001,
          11977.57496,
          2749.320965,
          9809.185636,
          4172.838464,
          7408.905561,
          19328.70901,
          18008.50924,
          42951.65309,
          10611.46299,
          11415.805690000001
         ],
         "y": [
          75.32,
          65.554,
          72.39,
          80.653,
          78.553,
          72.889,
          78.782,
          78.273,
          72.235,
          74.994,
          71.878,
          70.259,
          60.916000000000004,
          70.19800000000001,
          72.567,
          76.195,
          72.899,
          75.53699999999999,
          71.752,
          71.421,
          78.74600000000001,
          69.819,
          78.242,
          76.384,
          73.747
         ]
        },
        {
         "marker": {
          "color": "red",
          "line": {
           "width": 1
          },
          "size": 12
         },
         "mode": "markers",
         "name": "Europe",
         "text": [
          "Country: Albania<br>Population: 3600523.0",
          "Country: Austria<br>Population: 8199783.0",
          "Country: Belgium<br>Population: 10392226.0",
          "Country: Bosnia and Herzegovina<br>Population: 4552198.0",
          "Country: Bulgaria<br>Population: 7322858.0",
          "Country: Croatia<br>Population: 4493312.0",
          "Country: Czech Republic<br>Population: 10228744.0",
          "Country: Denmark<br>Population: 5468120.0",
          "Country: Finland<br>Population: 5238460.0",
          "Country: France<br>Population: 61083916.0",
          "Country: Germany<br>Population: 82400996.0",
          "Country: Greece<br>Population: 10706290.0",
          "Country: Hungary<br>Population: 9956108.0",
          "Country: Iceland<br>Population: 301931.0",
          "Country: Ireland<br>Population: 4109086.0",
          "Country: Italy<br>Population: 58147733.0",
          "Country: Montenegro<br>Population: 684736.0",
          "Country: Netherlands<br>Population: 16570613.0",
          "Country: Norway<br>Population: 4627926.0",
          "Country: Poland<br>Population: 38518241.0",
          "Country: Portugal<br>Population: 10642836.0",
          "Country: Romania<br>Population: 22276056.0",
          "Country: Serbia<br>Population: 10150265.0",
          "Country: Slovak Republic<br>Population: 5447502.0",
          "Country: Slovenia<br>Population: 2009245.0",
          "Country: Spain<br>Population: 40448191.0",
          "Country: Sweden<br>Population: 9031088.0",
          "Country: Switzerland<br>Population: 7554661.0",
          "Country: Turkey<br>Population: 71158647.0",
          "Country: United Kingdom<br>Population: 60776238.0"
         ],
         "type": "scatter",
         "uid": "ef7be1eb-e072-4d8f-9375-e420ae8781ee",
         "x": [
          5937.029525999999,
          36126.4927,
          33692.60508,
          7446.298803,
          10680.79282,
          14619.222719999998,
          22833.30851,
          35278.41874,
          33207.0844,
          30470.0167,
          32170.37442,
          27538.41188,
          18008.94444,
          36180.789189999996,
          40675.99635,
          28569.7197,
          9253.896111,
          36797.93332,
          49357.19017,
          15389.924680000002,
          20509.64777,
          10808.47561,
          9786.534714,
          18678.31435,
          25768.25759,
          28821.0637,
          33859.74835,
          37506.419069999996,
          8458.276384,
          33203.26128
         ],
         "y": [
          76.423,
          79.829,
          79.441,
          74.852,
          73.005,
          75.748,
          76.486,
          78.332,
          79.313,
          80.657,
          79.406,
          79.483,
          73.33800000000001,
          81.757,
          78.885,
          80.546,
          74.543,
          79.762,
          80.196,
          75.563,
          78.098,
          72.476,
          74.002,
          74.663,
          77.926,
          80.941,
          80.884,
          81.70100000000001,
          71.777,
          79.425
         ]
        }
       ],
       "layout": {
        "hovermode": "closest",
        "title": "Life Expectancy v. Per Capita GDP, 2007",
        "xaxis": {
         "gridwidth": 2,
         "ticklen": 5,
         "title": "GDP per capita (2000 dollars)",
         "zeroline": false
        },
        "yaxis": {
         "gridwidth": 2,
         "ticklen": 5,
         "title": "Life Expectancy (years)"
        }
       }
      },
      "text/html": [
       "<div id=\"d892df7e-afc0-4225-8070-c95061795072\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"d892df7e-afc0-4225-8070-c95061795072\", [{\"marker\": {\"color\": \"navy\", \"line\": {\"width\": 1}, \"size\": 12}, \"mode\": \"markers\", \"name\": \"Americas\", \"text\": [\"Country: Argentina<br>Population: 40301927.0\", \"Country: Bolivia<br>Population: 9119152.0\", \"Country: Brazil<br>Population: 190010647.0\", \"Country: Canada<br>Population: 33390141.0\", \"Country: Chile<br>Population: 16284741.0\", \"Country: Colombia<br>Population: 44227550.0\", \"Country: Costa Rica<br>Population: 4133884.0\", \"Country: Cuba<br>Population: 11416987.0\", \"Country: Dominican Republic<br>Population: 9319622.0\", \"Country: Ecuador<br>Population: 13755680.0\", \"Country: El Salvador<br>Population: 6939688.0\", \"Country: Guatemala<br>Population: 12572928.0\", \"Country: Haiti<br>Population: 8502814.0\", \"Country: Honduras<br>Population: 7483763.0\", \"Country: Jamaica<br>Population: 2780132.0\", \"Country: Mexico<br>Population: 108700891.0\", \"Country: Nicaragua<br>Population: 5675356.0\", \"Country: Panama<br>Population: 3242173.0\", \"Country: Paraguay<br>Population: 6667147.0\", \"Country: Peru<br>Population: 28674757.0\", \"Country: Puerto Rico<br>Population: 3942491.0\", \"Country: Trinidad and Tobago<br>Population: 1056608.0\", \"Country: United States<br>Population: 301139947.0\", \"Country: Uruguay<br>Population: 3447496.0\", \"Country: Venezuela<br>Population: 26084662.0\"], \"x\": [12779.379640000001, 3822.1370840000004, 9065.800825, 36319.235010000004, 13171.63885, 7006.580419, 9645.06142, 8948.102923, 6025.374752000001, 6873.262326000001, 5728.353514, 5186.050003, 1201.637154, 3548.3308460000003, 7320.880262000001, 11977.57496, 2749.320965, 9809.185636, 4172.838464, 7408.905561, 19328.70901, 18008.50924, 42951.65309, 10611.46299, 11415.805690000001], \"y\": [75.32, 65.554, 72.39, 80.653, 78.553, 72.889, 78.782, 78.273, 72.235, 74.994, 71.878, 70.259, 60.916000000000004, 70.19800000000001, 72.567, 76.195, 72.899, 75.53699999999999, 71.752, 71.421, 78.74600000000001, 69.819, 78.242, 76.384, 73.747], \"type\": \"scatter\", \"uid\": \"c2bae803-f572-4c0a-8e45-6874fec3b870\"}, {\"marker\": {\"color\": \"red\", \"line\": {\"width\": 1}, \"size\": 12}, \"mode\": \"markers\", \"name\": \"Europe\", \"text\": [\"Country: Albania<br>Population: 3600523.0\", \"Country: Austria<br>Population: 8199783.0\", \"Country: Belgium<br>Population: 10392226.0\", \"Country: Bosnia and Herzegovina<br>Population: 4552198.0\", \"Country: Bulgaria<br>Population: 7322858.0\", \"Country: Croatia<br>Population: 4493312.0\", \"Country: Czech Republic<br>Population: 10228744.0\", \"Country: Denmark<br>Population: 5468120.0\", \"Country: Finland<br>Population: 5238460.0\", \"Country: France<br>Population: 61083916.0\", \"Country: Germany<br>Population: 82400996.0\", \"Country: Greece<br>Population: 10706290.0\", \"Country: Hungary<br>Population: 9956108.0\", \"Country: Iceland<br>Population: 301931.0\", \"Country: Ireland<br>Population: 4109086.0\", \"Country: Italy<br>Population: 58147733.0\", \"Country: Montenegro<br>Population: 684736.0\", \"Country: Netherlands<br>Population: 16570613.0\", \"Country: Norway<br>Population: 4627926.0\", \"Country: Poland<br>Population: 38518241.0\", \"Country: Portugal<br>Population: 10642836.0\", \"Country: Romania<br>Population: 22276056.0\", \"Country: Serbia<br>Population: 10150265.0\", \"Country: Slovak Republic<br>Population: 5447502.0\", \"Country: Slovenia<br>Population: 2009245.0\", \"Country: Spain<br>Population: 40448191.0\", \"Country: Sweden<br>Population: 9031088.0\", \"Country: Switzerland<br>Population: 7554661.0\", \"Country: Turkey<br>Population: 71158647.0\", \"Country: United Kingdom<br>Population: 60776238.0\"], \"x\": [5937.029525999999, 36126.4927, 33692.60508, 7446.298803, 10680.79282, 14619.222719999998, 22833.30851, 35278.41874, 33207.0844, 30470.0167, 32170.37442, 27538.41188, 18008.94444, 36180.789189999996, 40675.99635, 28569.7197, 9253.896111, 36797.93332, 49357.19017, 15389.924680000002, 20509.64777, 10808.47561, 9786.534714, 18678.31435, 25768.25759, 28821.0637, 33859.74835, 37506.419069999996, 8458.276384, 33203.26128], \"y\": [76.423, 79.829, 79.441, 74.852, 73.005, 75.748, 76.486, 78.332, 79.313, 80.657, 79.406, 79.483, 73.33800000000001, 81.757, 78.885, 80.546, 74.543, 79.762, 80.196, 75.563, 78.098, 72.476, 74.002, 74.663, 77.926, 80.941, 80.884, 81.70100000000001, 71.777, 79.425], \"type\": \"scatter\", \"uid\": \"ef7be1eb-e072-4d8f-9375-e420ae8781ee\"}], {\"hovermode\": \"closest\", \"title\": \"Life Expectancy v. Per Capita GDP, 2007\", \"xaxis\": {\"gridwidth\": 2, \"ticklen\": 5, \"title\": \"GDP per capita (2000 dollars)\", \"zeroline\": false}, \"yaxis\": {\"gridwidth\": 2, \"ticklen\": 5, \"title\": \"Life Expectancy (years)\"}}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"d892df7e-afc0-4225-8070-c95061795072\"));});</script>"
      ],
      "text/vnd.plotly.v1+html": [
       "<div id=\"d892df7e-afc0-4225-8070-c95061795072\" style=\"height: 525px; width: 100%;\" class=\"plotly-graph-div\"></div><script type=\"text/javascript\">require([\"plotly\"], function(Plotly) { window.PLOTLYENV=window.PLOTLYENV || {};window.PLOTLYENV.BASE_URL=\"https://plot.ly\";Plotly.newPlot(\"d892df7e-afc0-4225-8070-c95061795072\", [{\"marker\": {\"color\": \"navy\", \"line\": {\"width\": 1}, \"size\": 12}, \"mode\": \"markers\", \"name\": \"Americas\", \"text\": [\"Country: Argentina<br>Population: 40301927.0\", \"Country: Bolivia<br>Population: 9119152.0\", \"Country: Brazil<br>Population: 190010647.0\", \"Country: Canada<br>Population: 33390141.0\", \"Country: Chile<br>Population: 16284741.0\", \"Country: Colombia<br>Population: 44227550.0\", \"Country: Costa Rica<br>Population: 4133884.0\", \"Country: Cuba<br>Population: 11416987.0\", \"Country: Dominican Republic<br>Population: 9319622.0\", \"Country: Ecuador<br>Population: 13755680.0\", \"Country: El Salvador<br>Population: 6939688.0\", \"Country: Guatemala<br>Population: 12572928.0\", \"Country: Haiti<br>Population: 8502814.0\", \"Country: Honduras<br>Population: 7483763.0\", \"Country: Jamaica<br>Population: 2780132.0\", \"Country: Mexico<br>Population: 108700891.0\", \"Country: Nicaragua<br>Population: 5675356.0\", \"Country: Panama<br>Population: 3242173.0\", \"Country: Paraguay<br>Population: 6667147.0\", \"Country: Peru<br>Population: 28674757.0\", \"Country: Puerto Rico<br>Population: 3942491.0\", \"Country: Trinidad and Tobago<br>Population: 1056608.0\", \"Country: United States<br>Population: 301139947.0\", \"Country: Uruguay<br>Population: 3447496.0\", \"Country: Venezuela<br>Population: 26084662.0\"], \"x\": [12779.379640000001, 3822.1370840000004, 9065.800825, 36319.235010000004, 13171.63885, 7006.580419, 9645.06142, 8948.102923, 6025.374752000001, 6873.262326000001, 5728.353514, 5186.050003, 1201.637154, 3548.3308460000003, 7320.880262000001, 11977.57496, 2749.320965, 9809.185636, 4172.838464, 7408.905561, 19328.70901, 18008.50924, 42951.65309, 10611.46299, 11415.805690000001], \"y\": [75.32, 65.554, 72.39, 80.653, 78.553, 72.889, 78.782, 78.273, 72.235, 74.994, 71.878, 70.259, 60.916000000000004, 70.19800000000001, 72.567, 76.195, 72.899, 75.53699999999999, 71.752, 71.421, 78.74600000000001, 69.819, 78.242, 76.384, 73.747], \"type\": \"scatter\", \"uid\": \"c2bae803-f572-4c0a-8e45-6874fec3b870\"}, {\"marker\": {\"color\": \"red\", \"line\": {\"width\": 1}, \"size\": 12}, \"mode\": \"markers\", \"name\": \"Europe\", \"text\": [\"Country: Albania<br>Population: 3600523.0\", \"Country: Austria<br>Population: 8199783.0\", \"Country: Belgium<br>Population: 10392226.0\", \"Country: Bosnia and Herzegovina<br>Population: 4552198.0\", \"Country: Bulgaria<br>Population: 7322858.0\", \"Country: Croatia<br>Population: 4493312.0\", \"Country: Czech Republic<br>Population: 10228744.0\", \"Country: Denmark<br>Population: 5468120.0\", \"Country: Finland<br>Population: 5238460.0\", \"Country: France<br>Population: 61083916.0\", \"Country: Germany<br>Population: 82400996.0\", \"Country: Greece<br>Population: 10706290.0\", \"Country: Hungary<br>Population: 9956108.0\", \"Country: Iceland<br>Population: 301931.0\", \"Country: Ireland<br>Population: 4109086.0\", \"Country: Italy<br>Population: 58147733.0\", \"Country: Montenegro<br>Population: 684736.0\", \"Country: Netherlands<br>Population: 16570613.0\", \"Country: Norway<br>Population: 4627926.0\", \"Country: Poland<br>Population: 38518241.0\", \"Country: Portugal<br>Population: 10642836.0\", \"Country: Romania<br>Population: 22276056.0\", \"Country: Serbia<br>Population: 10150265.0\", \"Country: Slovak Republic<br>Population: 5447502.0\", \"Country: Slovenia<br>Population: 2009245.0\", \"Country: Spain<br>Population: 40448191.0\", \"Country: Sweden<br>Population: 9031088.0\", \"Country: Switzerland<br>Population: 7554661.0\", \"Country: Turkey<br>Population: 71158647.0\", \"Country: United Kingdom<br>Population: 60776238.0\"], \"x\": [5937.029525999999, 36126.4927, 33692.60508, 7446.298803, 10680.79282, 14619.222719999998, 22833.30851, 35278.41874, 33207.0844, 30470.0167, 32170.37442, 27538.41188, 18008.94444, 36180.789189999996, 40675.99635, 28569.7197, 9253.896111, 36797.93332, 49357.19017, 15389.924680000002, 20509.64777, 10808.47561, 9786.534714, 18678.31435, 25768.25759, 28821.0637, 33859.74835, 37506.419069999996, 8458.276384, 33203.26128], \"y\": [76.423, 79.829, 79.441, 74.852, 73.005, 75.748, 76.486, 78.332, 79.313, 80.657, 79.406, 79.483, 73.33800000000001, 81.757, 78.885, 80.546, 74.543, 79.762, 80.196, 75.563, 78.098, 72.476, 74.002, 74.663, 77.926, 80.941, 80.884, 81.70100000000001, 71.777, 79.425], \"type\": \"scatter\", \"uid\": \"ef7be1eb-e072-4d8f-9375-e420ae8781ee\"}], {\"hovermode\": \"closest\", \"title\": \"Life Expectancy v. Per Capita GDP, 2007\", \"xaxis\": {\"gridwidth\": 2, \"ticklen\": 5, \"title\": \"GDP per capita (2000 dollars)\", \"zeroline\": false}, \"yaxis\": {\"gridwidth\": 2, \"ticklen\": 5, \"title\": \"Life Expectancy (years)\"}}, {\"showLink\": true, \"linkText\": \"Export to plot.ly\", \"plotlyServerURL\": \"https://plot.ly\"})});</script><script type=\"text/javascript\">window.addEventListener(\"resize\", function(){window._Plotly.Plots.resize(document.getElementById(\"d892df7e-afc0-4225-8070-c95061795072\"));});</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 一个更复杂的例子\n",
    "df = pd.read_csv('https://raw.githubusercontent.com/yankev/test/master/life-expectancy-per-GDP-2007.csv')\n",
    "\n",
    "americas = df[(df.continent=='Americas')]\n",
    "europe = df[(df.continent=='Europe')]\n",
    "\n",
    "trace_comp0 = go.Scatter(\n",
    "    x=americas.gdp_percap,\n",
    "    y=americas.life_exp,\n",
    "    mode='markers',\n",
    "    marker=dict(size=12,\n",
    "                line=dict(width=1),\n",
    "                color=\"navy\"\n",
    "               ),\n",
    "    name='Americas',\n",
    "    text=americas.country,\n",
    "    )\n",
    "\n",
    "trace_comp1 = go.Scatter(\n",
    "    x=europe.gdp_percap,\n",
    "    y=europe.life_exp,\n",
    "    mode='markers',\n",
    "    marker=dict(size=12,\n",
    "                line=dict(width=1),\n",
    "                color=\"red\"\n",
    "               ),\n",
    "    name='Europe',\n",
    "    text=europe.country,\n",
    "        )\n",
    "\n",
    "data_comp = [trace_comp0, trace_comp1]\n",
    "layout_comp = go.Layout(\n",
    "    title='Life Expectancy v. Per Capita GDP, 2007',\n",
    "    hovermode='closest',\n",
    "    xaxis=dict(\n",
    "        title='GDP per capita (2000 dollars)',\n",
    "        ticklen=5,\n",
    "        zeroline=False,\n",
    "        gridwidth=2,\n",
    "    ),\n",
    "    yaxis=dict(\n",
    "        title='Life Expectancy (years)',\n",
    "        ticklen=5,\n",
    "        gridwidth=2,\n",
    "    ),\n",
    ")\n",
    "fig_comp = go.Figure(data=data_comp, layout=layout_comp)\n",
    "plotly.offline.iplot(fig_comp, filename='life-expectancy-per-GDP-2007')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
